package app.fugema.shop.main.goods.view;

import com.base.common.app.LoginUtils;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.MyGridLayoutManager;
import com.base.common.view.base.BaseTitleActivity;
import com.base.common.view.base.OnGetDataListener;
import com.base.common.viewmodel.BaseViewObserver;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import app.fugema.shop.R;
import app.fugema.shop.databinding.ActivityMiniShopBinding;
import app.fugema.shop.main.goods.adapter.SimilarGoodsAdapter;
import app.fugema.shop.main.goods.model.SimilarGoodsResult;
import app.fugema.shop.main.goods.vm.GoodsVM;

//看相似商品页面
public class SimilarGoodsActivity extends BaseTitleActivity<ActivityMiniShopBinding, GoodsVM> implements OnGetDataListener
{
    private String userKey = "";

    public static void start(Context context , int productId)
    {
        Intent intent = new Intent(context,SimilarGoodsActivity.class);
        intent.putExtra("goodsId", productId);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mini_shop);
    }

    private int lastProductId ;
    private MyGridLayoutManager mGridLayoutMgr;

    private SimilarGoodsAdapter goodsAdapter;

    @Override
    protected int getStatusBarColor()
    {
        return R.color.redColor;
    }

    @Override
    protected void initView()
    {
        super.initView();
        if (LoginUtils.isLogin())
        {
            userKey = LoginUtils.getToken();
        }
        setCloseImg(R.mipmap.ic_back_white);
        setTitle("相似商品",R.color.white);

        mGridLayoutMgr = new MyGridLayoutManager(this, 2);
        binding.rvRecyclerView.setLayoutManager(mGridLayoutMgr);
        goodsAdapter = new SimilarGoodsAdapter();
        binding.rvRecyclerView.setAdapter(goodsAdapter);

        viewUtils.setRefreshStateLayout(binding.rvRecyclerView, this, this);
        viewUtils.setAutoPreload(false);
    }


    @Override
    protected void initData(Intent intent)
    {
        super.initData(intent);
        lastProductId = intent.getIntExtra("goodsId",0);

        onGetData(1);
    }


    @Override
    public void onGetData(int pn)
    {
        viewModel.getSimilarProduct(lastProductId ,pn)
                .observe(this, new BaseViewObserver<LiveDataWrapper<SimilarGoodsResult>>(viewUtils.getStateLayout())
                {
                    @Override
                    protected void onSuccess(LiveDataWrapper<SimilarGoodsResult> data)
                    {
                        //搜索到 商品

                        viewUtils.setDataListRefreshLayout(goodsAdapter,pn, 10, data.data.getData());
//                goodsAdapter.setDataList( new ArrayList<SearchGoodsBean.RecordsBean>() );
                    }

                    @Override
                    public boolean isEmptyData(LiveDataWrapper<SimilarGoodsResult> data)
                    {
                        return pn == 1 && UIUtils.isEmpty(data.data.getData());
                    }

                    @Override
                    public boolean isRefreshLayoutData() {
                        return true;
                    }
                });


    }



}
